<template>
  <div class="dialog-form">
    <!-- 添加或修改弹出框 -->
    <el-dialog
      :title="title"
      center
      :visible.sync="open"
      @close="close()"
      width="660px"
      append-to-body
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item
          :label="$t('batch.timingConfig.form.customName')"
          prop="friendlyName"
        >
          <el-input
            v-model="editForm.friendlyName"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
            :disabled="disabled"
          />
        </el-form-item>
        <el-form-item
          :label="$t('batch.missionManage.form.processCode')"
          prop="flowCode"
        >
          <el-select
            :disabled="disabled"
            v-model="editForm.flowCode"
            :placeholder="$t('batch.missionManage.rules.pleaseSelect')"
            style="width: 100%"
          >
            <el-option
              v-for="item in workflowList"
              :key="item.flowCode"
              :label="item.flowName"
              :value="item.flowCode"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          :label="$t('batch.timingConfig.form.expressionType')"
          prop="expressionType"
        >
          <el-select
            :disabled="disabled"
            v-model="editForm.expressionType"
            :placeholder="$t('batch.missionManage.rules.pleaseSelect')"
            style="width: 100%"
          >
            <el-option
              v-for="item in dictMap.expression_type"
              :key="item.value"
              :label="item.text"
              :value="Number(item.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :label="$t('batch.timingConfig.form.executionTime')"
          prop="timedExpression"
        >
          <el-input
            v-model="editForm.timedExpression"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
            :disabled="disabled"
          />
        </el-form-item>
        <el-form-item
          :label="$t('batch.timingConfig.form.whetherToRelyOn')"
          prop="cronTaskBlockingStrategy"
        >
          <el-radio-group
            v-model="editForm.cronTaskBlockingStrategy"
            :disabled="disabled"
          >
            <el-radio key="2" :label="2">{{ $t("common.yes") }}</el-radio>
            <el-radio key="1" :label="1">{{ $t("common.no") }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!disabled">{{
          $t("common.confirm")
        }}</el-button>
        <el-button @click="close">{{ $t("common.cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  get,
  save,
  update,
  checkTaskTimedFlowCodeUnique,
  checkTaskTimedNameUnique,
} from "@/api/batch_app/timingConfig";
import { queryAllFlowConf } from "@/api/batch_app/workflowConfig";
import { dialogMixin } from "@/libs/dialogMixin";
export default {
  name: "EditTimingConfig",
  mixins: [dialogMixin],
  data() {
    // 表单校验
    let checkTaskTimedFlowCodeUniqueRule = (rule, value, callback) => {
      if (!value) {
        callback(new Error(this.$t("batch.systemConfig.rules.mustChoose")));
      } else {
        checkTaskTimedFlowCodeUnique({
          flowCode: this.editForm.flowCode,
          id: this.editForm.id,
        }).then((response) => {
          var result = response.result;
          if (!result) {
            callback(
              new Error(this.$t("batch.systemConfig.rules.alreadyExists"))
            );
          } else {
            callback();
          }
        });
      }
    };

    let checkTaskTimedNameUniqueRule = (rule, value, callback) => {
      if (!value) {
        callback(new Error(this.$t("batch.systemConfig.rules.cannotEmpty")));
      } else {
        checkTaskTimedNameUnique({
          taskName: value,
          id: this.editForm.id,
        }).then((response) => {
          var result = response.result;
          if (!result) {
            callback(
              new Error(this.$t("batch.systemConfig.rules.alreadyExists"))
            );
          } else {
            callback();
          }
        });
      }
    };
    return {
      disabled: false,
      workflowList: [],
      rules: {
        friendlyName: [
          {
            required: true,
            trigger: "blur",
            validator: checkTaskTimedNameUniqueRule,
          },
        ],
        flowCode: [
          {
            required: true,
            trigger: "change",
            validator: checkTaskTimedFlowCodeUniqueRule,
          },
        ],
        timedExpression: [
          {
            required: true,
            message: this.$t("batch.systemConfig.rules.cannotEmpty"),
            trigger: "blur",
          },
        ],
        expressionType: [
          {
            required: true,
            message: this.$t("batch.systemConfig.rules.cannotEmpty"),
            trigger: "blur",
          },
        ],
        cronTaskBlockingStrategy: [
          {
            required: true,
            message: this.$t("batch.systemConfig.rules.mustChoose"),
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    // 此处做数据字典数据加载初始化操作
    this.loadDicts(["expression_type"]);
    queryAllFlowConf().then((res) => {
      this.workflowList = res.result;
    });
  },
  methods: {
    show(id, disabled = false) {
      this.disabled = disabled;
      this.$set(this, "editForm", { cronTaskBlockingStrategy: 1 });
      if (id) {
        get(id).then((res) => {
          this.$set(this, "editForm", res.result);
          this.open = true;
        });
      } else {
        this.open = true;
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          if (this.editForm.id) {
            delete this.editForm.createTime;
            delete this.editForm.updateTime;
            update(this.editForm).then((res) => {
              this.msgSuccess(this.$t("common.modifySuccess"));
              this.close(true);
            });
          } else {
            delete this.editForm.createTime;
            delete this.editForm.updateTime;
            save(this.editForm).then((res) => {
              this.msgSuccess(this.$t("common.addSuccess"));
              this.close(true);
            });
          }
        }
      });
    },
    // 取消按钮
    close(state = false) {
      this.open = false;
      this.resetForm("editForm");
      this.$emit("editComplete", state);
    },
  },
};
</script>

<style scoped>
</style>

